<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '100px'"
    >
      <!-- <el-form-item label="房源id" prop="houseId">
                <el-input v-model="dataForm.houseId" placeholder="房源id"></el-input>
      </el-form-item>-->
      <el-row>
        <el-col :span="12">
          <el-form-item label="委托人部门">
            <ren-dept-tree v-model="dataForm.deptId" :placeholder="$t('dept.title')"></ren-dept-tree>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="委托人" prop="entrustUser">
            <el-select v-model="dataForm.entrustUser" style="width:100%" placeholder="委托人">
              <el-option
                v-for="item in UserList"
                :key="item.id"
                :label="item.realName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="责任人部门">
            <ren-dept-tree v-model="dataForm.ZRRId" :placeholder="$t('dept.title')"></ren-dept-tree>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="责任人" prop="userId">
            <el-select v-model="dataForm.userId" style="width:100%" v-if="!dataForm.id">
              <el-option
                v-for="item in UserList2"
                :key="item.id"
                :label="item.realName"
                :value="item.id"
              ></el-option>
            </el-select>
            <el-select v-else v-model="dataForm.userId" placeholder="请选择" style="width:100%">
              <el-option
                v-for="item in this.$store.state.maintainList"
                :key="item.id"
                :label="item.realName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="委托日期" prop="entrustDate">
            <el-date-picker
              v-model="dataForm.entrustDate"
              type="date"
              placeholder="选择日期"
              value-format="timestamp"
              style="width:100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="委托开始期限" prop="entrustTers">
            <el-date-picker
              v-model="dataForm.entrustTers"
              type="date"
              placeholder="选择日期"
              value-format="timestamp"
              style="width:100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="委托结束期限" prop="entrustTere">
            <el-date-picker
              v-model="dataForm.entrustTere"
              type="date"
              placeholder="选择日期"
              value-format="timestamp"
              style="width:100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="协议编号" prop="number">
            <el-input v-model="dataForm.number" placeholder="协议编号"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="产权面积" prop="occupyArea">
            <el-input v-model="dataForm.occupyArea" placeholder="产权面积"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="使用面积" prop="floorArea">
            <el-input v-model="dataForm.floorArea" placeholder="使用面积"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="总价" prop="prices">
            <el-input v-model="dataForm.prices" placeholder="总价"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单价" prop="price">
            <el-input v-model="dataForm.price" placeholder="单价"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="底价" prop="floorPrice">
            <el-input v-model="dataForm.floorPrice" placeholder="底价"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="保证金" prop="deposit">
            <el-input v-model="dataForm.deposit" placeholder="保证金"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="付款方式" prop="payType">
            <!-- <el-input v-model="dataForm.payType" placeholder="付款方式"></el-input> -->
            <ren-select
              v-if="transactionType == '0'"
              v-model="dataForm.payType"
              dict-type="dictOwnerBuy"
              placeholder="付款方式"
              style="width:100%"
            ></ren-select>
            <ren-select
              v-else-if="transactionType == '1'"
              v-model="dataForm.payType"
              dict-type="dictTenanPay"
              placeholder="付款方式"
              style="width:100%"
            ></ren-select>
            <ren-select
              v-else
              v-model="dataForm.payType"
              dict-type="dictOwnerBuy"
              placeholder="付款方式"
              style="width:100%"
            ></ren-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同年限" prop="tenanContractTime">
            <el-input v-model="dataForm.tenanContractTime" placeholder="合同年限"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="递增" prop="increase">
            <el-input v-model="dataForm.increase" placeholder="递增"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="免租时间" prop="tenanFreeTime">
            <el-input v-model="dataForm.tenanFreeTime" placeholder="免租时间"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="佣金" prop="servicePrice">
            <el-input v-model="dataForm.servicePrice" placeholder="佣金"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="房屋状态" prop="situation">
            <!-- <el-input v-model="dataForm.situation" placeholder="房屋状态"></el-input> -->
            <ren-select
              v-model="dataForm.situation"
              dict-type="dictSituation"
              class="FormSelect"
              style="width:100%"
            ></ren-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remake">
            <el-input v-model="dataForm.remake" placeholder="备注" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      transactionType: this.$route.query.transactionType,
      type: this.$route.query.type,
      dataForm: {
        id: "",
        houseId: this.houseId,
        entrustUser: "",
        entrustDate: "",
        entrustTers: "",
        entrustTere: "",
        number: "",
        ZRRId: "",
        userId: "",
        deptId: "",
        occupyArea: "",
        floorArea: "",
        prices: "",
        price: "",
        floorPrice: "",
        deposit: "",
        payType: "",
        tenanContractTime: "",
        increase: "",
        tenanFreeTime: "",
        servicePrice: "",
        remake: "",
        situation: "",
        creator: "",
        createDate: "",
        updater: "",
        updateDate: ""
      },
      UserList: [],
      UserList2: [],
      dialogImageUrl: "",
      dialogVisible: false
    };
  },
  computed: {
    dataRule() {
      return {
        houseId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        entrustUser: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        entrustDate: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        entrustTers: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        entrustTere: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        number: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        userId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        deptId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        occupyArea: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        floorArea: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        prices: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        price: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        floorPrice: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        deposit: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        payType: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        tenanContractTime: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        increase: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        tenanFreeTime: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        servicePrice: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        remake: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        situation: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        updater: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        updateDate: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ]
      };
    }
  },
  props: ["houseId"],
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();

        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/house/houseentrust/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
        })
        .catch(() => {});
    },

    // 获取部门员工列表
    getDeptUser() {
      this.$http
        .get(`/sys/user/deptUserList`, {
          params: {
            deptId: this.dataForm.deptId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.UserList = res.data;
        })
        .catch(() => {});
    },

    // 获取部门员工列表
    getDeptUser2() {
      this.$http
        .get(`/sys/user/deptUserList`, {
          params: {
            deptId: this.dataForm.ZRRId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.UserList2 = res.data;
        })
        .catch(() => {});
    },

    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/house/houseentrust/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    )
  },
  watch: {
    "dataForm.deptId": {
      handler(nowVal, oldVal) {
        this.getDeptUser();
      }
    },
    "dataForm.ZRRId": {
      handler(nowVal, oldVal) {
        this.getDeptUser2();
      }
    }
  }
};
</script>
